package com.sams.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.sams.entity.Processmanage;
import com.sams.entity.ProcessmanageExample;
import com.sams.entity.School;
import com.sams.entity.ProcessmanageExample.Criteria;
import com.sams.mapper.ProcessmanageMapper;
import com.sams.mapper.SchoolMapper;
import com.sams.entity.User;

import com.sams.service.ProcessmanageService;
@Service
public class ProcessmanageServiceImpl implements ProcessmanageService{

	@Autowired
	private ProcessmanageMapper mapper;
	@Autowired
	private SchoolMapper schoolMapper;
	
	//查询分页
	public PageInfo<Processmanage> ListPro(int currentPage,Integer schild,Integer status, User user,Integer zid) {
		
		ProcessmanageExample example=new ProcessmanageExample();
		Criteria createCriteria=example.createCriteria();
		//1.roleid=1校级用户 uid=1且 查询的status=1，2，3的数据
		if(user.getRoleid()==1) {
			createCriteria.andUidEqualTo(user.getId());			
			 createCriteria.andStatusNotEqualTo(4);  
		 
		}else if(user.getRoleid()==2) {
			//2.roledid=2镇级用户  查镇下面的学校 再查status1，2，3
			 List<Integer> scIds = new ArrayList<Integer>();
			 List<School> scList = schoolMapper.selectSchoolByUid(user.getId());
			for(School sc : scList) {
					scIds.add(sc.getSchid());
				}
				createCriteria.andSchidIn(scIds);
			/* createCriteria.andStatusNotEqualTo(4); */
				createCriteria.andStatusEqualTo(2); //只需要它审核的数据  
		} else {
			//， 3：县账号，4 ：管理员
			 if(user.getRoleid()==3) {// 3//镇账户查询状态： 县审核重点3  //只需要它审核的数据    其他县不考虑
				createCriteria.andStatusEqualTo(3);
			}
			 //根据镇id 也是查询所有学校
			 if(zid!=null &&  !zid.equals("")) {
				 List<Integer> scIds = new ArrayList<Integer>();
				 List<School> scList = schoolMapper.selectSchoolByZid(zid);
				 for(School sc : scList) {
					 scIds.add(sc.getSchid());
				 }
				 createCriteria.andSchidIn(scIds);
			 }
		}
		
		//按照条件查询
		if(schild!=null && !"".equals(schild)) {
			createCriteria.andSchidEqualTo(schild);
		}
		if(status!=null && !"".equals(status)) {
			createCriteria.andStatusEqualTo(status);
		}
		// 查询不等于4的
		createCriteria.andStatusNotEqualTo(4);
		//设置分页
		PageHelper.startPage(currentPage, 20);
		List<Processmanage> selectByExample = mapper.selectByExample(example);
		PageInfo<Processmanage> pageInfo=new PageInfo<Processmanage>(selectByExample);
		
		return pageInfo;
	}

	public int add(Processmanage pro) {
		
		
		return mapper.insert(pro);
	}

	public int delete(int id) {
		if(mapper.selectByPrimaryKey(id).getStatus()==1) {
			return mapper.deleteByPrimaryKey(id);
		}else {
			return 0;
		}
		
	}
	//上报
	public int update(Processmanage pro) {
		// TODO Auto-generated method stub
		
		return mapper.updateByPrimaryKey(pro);
	}

	public Processmanage findById(int id) {
		// TODO Auto-generated method stub
		return mapper.selectByPrimaryKey(id);
	}

	public int report(Integer id) {
		return mapper.report(id);
 	}
	
	public int noreport(Integer id) {
		return mapper.noreport(id);
 	}

}
